package com.sk.sourcecircle.adapter;

import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.chat.EMTextMessageBody;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.entity.MyEMConversation;
import com.sk.sourcecircle.utils.MyUtils;

import java.util.List;

/**
 * Created by sk on 2018/1/4.
 */

public class ChatListAdapter extends BaseMultiItemQuickAdapter<MyEMConversation, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ChatListAdapter(List<MyEMConversation> data) {
        super(data);
        addItemType(1, R.layout.item_person_chat);
        addItemType(2, R.layout.item_group_chat);
    }

    @Override
    //
    protected void convert(BaseViewHolder helper, MyEMConversation item) {

        if (item.getConversation().getUnreadMsgCount() > 0) {
            helper.setGone(R.id.red_circle, true);
        } else {
            helper.setGone(R.id.red_circle, false);
        }

        switch (item.getItemType()) {
            case 1:
                helper.addOnClickListener(R.id.head_img);

                if (item.getConversation().getLastMessage().getFrom().equals(MyUtils.getUser(mContext).getHx_login())) {
                    if (item.getConversation().getLastMessage() != null && item.getConversation().getLastMessage().ext() != null) {
                        Glide.with(mContext).load(item.getConversation().getLastMessage().ext().get("toUrl")).apply(RequestOptions.bitmapTransform(new CircleCrop())).into((ImageView) helper.getView(R.id.head_img));
                        helper.setText(R.id.tv_name, "" + item.getConversation().getLastMessage().ext().get("toName"));
                    }
                } else {
                    if (item.getConversation().getLastMessage() != null && item.getConversation().getLastMessage().ext() != null) {
                        Glide.with(mContext).load(item.getConversation().getLastMessage().ext().get("head_url")).apply(RequestOptions.bitmapTransform(new CircleCrop())).into((ImageView) helper.getView(R.id.head_img));
                        helper.setText(R.id.tv_name, "" + item.getConversation().getLastMessage().ext().get("nickname"));
                    }
                }


                helper.setText(R.id.tv_time, MyUtils.getTimeFormatText(item.getConversation().getLastMessage().getMsgTime()));
                switch (item.getConversation().getLastMessage().getType()) {
                    case TXT:
                        EMTextMessageBody body = (EMTextMessageBody) item.getConversation().getLastMessage().getBody();
                        helper.setText(R.id.tv_content, body.getMessage());
                        break;
                    case IMAGE:
                        helper.setText(R.id.tv_content, "[图片]");
                        break;
                    case VOICE:
                        helper.setText(R.id.tv_content, "[语音]");
                        break;
                    case VIDEO:
                        helper.setText(R.id.tv_content, "[视频]");
                        break;
                }
                break;
            case 2:

                if (item.getConversation().getLastMessage() != null && item.getConversation().getLastMessage().ext() != null) {
                    Glide.with(mContext).load(item.getConversation().getLastMessage().ext().get("toUrl")).into((ImageView) helper.getView(R.id.head_img));
                    helper.setText(R.id.tv_name, "" + item.getConversation().getLastMessage().ext().get("toName"))
                            .setText(R.id.group_pep, "来自" + item.getConversation().getLastMessage().ext().get("groupName"));
                    helper.setText(R.id.tv_time, MyUtils.getTimeFormatText(item.getConversation().getLastMessage().getMsgTime()));

                }
                switch (item.getConversation().getLastMessage().getType()) {
                    case TXT:
                        EMTextMessageBody body = (EMTextMessageBody) item.getConversation().getLastMessage().getBody();
                        helper.setText(R.id.tv_content, "【" + item.getConversation().getUnreadMsgCount() + "】条  " + item.getConversation().getLastMessage().ext().get("nickname") + "：" + body.getMessage());
                        break;
                    case IMAGE:
                        helper.setText(R.id.tv_content, "【" + item.getConversation().getUnreadMsgCount() + "】条  " + item.getConversation().getLastMessage().ext().get("nickname") + "：[图片]");
                        break;
                    case VOICE:
                        helper.setText(R.id.tv_content, "【" + item.getConversation().getUnreadMsgCount() + "】条  " + item.getConversation().getLastMessage().ext().get("nickname") + "：[语音]");
                        break;
                    case VIDEO:
                        helper.setText(R.id.tv_content, "[视频]");
                        break;
                }

                break;
        }
    }
}
